/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2010, 2012 Oracle and/or its affiliates.  All rights reserved.
 *
 */

package tools;

import oracle.kv.KVStore;
import oracle.kv.KVStoreConfig;
import oracle.kv.KVStoreFactory;
import oracle.kv.Key;
import oracle.kv.Value;
import oracle.kv.ValueVersion;
import oracle.kv.Version;

import java.lang.Integer;

/**
 * Classe responsable de l'initialisation des donnees dans la partie 2
 * @author Guerline Jean-Baptiste & Mouzaffar Akbaraly 
 */
public class DataInitializerBEquilibre {

	private final KVStore store1;
	private final KVStore store2;

	public static void main(String args[]) {
		try {
			new DataInitializerBEquilibre();
			//example.runExample();
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Initialise les stores necessaires
	 */
	DataInitializerBEquilibre() {

		String storeName1 = "kvstore";
		String hostName1 = "ari-31-207-09";
		String hostPort1 = "5000";

		String storeName2 = "kvstore";
		String hostName2 = "ari-31-207-11";
		String hostPort2 = "5000";

		store1 = KVStoreFactory.getStore
				(new KVStoreConfig(storeName1, hostName1 + ":" + hostPort1));
		store2 = KVStoreFactory.getStore
				(new KVStoreConfig(storeName2, hostName2 + ":" + hostPort2));
		for(int i = 1; i <= 1000 ; i++){
			final String keyCategoryString = "C1";
			final String keyString = "P" + i;
			final String valueString = "1";
			
			//ajout des references dans chaque store vers le store "suivant" -> Liste Chainé: store A --> store B --> store A
			//																					   |-----------------------|
			store1.put(Key.createKey("storeNameSuivant"),
					Value.createValue(storeName2.getBytes()));
			store1.put(Key.createKey("hostPortSuivant"),
					Value.createValue(hostPort2.getBytes()));
			store1.put(Key.createKey("hostNameSuivant"),
					Value.createValue(hostName2.getBytes()));
			
			store2.put(Key.createKey("storeNameSuivant"),
					Value.createValue(storeName1.getBytes()));
			store2.put(Key.createKey("hostPortSuivant"),
					Value.createValue(hostPort1.getBytes()));
			store2.put(Key.createKey("hostNameSuivant"),
					Value.createValue(hostName1.getBytes()));
			
			if(i%2 == 0){
				store1.put(Key.createKey(keyCategoryString, keyString),
						Value.createValue(valueString.getBytes()));
			}
			else {
				store2.put(Key.createKey(keyCategoryString, keyString),
						Value.createValue(valueString.getBytes()));
			}
		}
		store1.close();
		store2.close();
	}

}
